package com.shenbianyun.cloudcaring;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shenbianyun.bosskgpay.utils.HttpClientHelper;
import com.shenbianyun.bosskgpay.utils.SnowflakeIdGeneratorFactory;
import com.shenbianyun.cloudcaring.payment.MD5SignUtils;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

import static com.shenbianyun.bosskgpay.utils.RSAUtils.CHARSET_UTF_8;

/**
 * @author ：张一凡
 * @date ：2025年03月15日 17:33:42
 * @description： 获取收银台URL
 */
public class PayCashierTest extends AbstractJavaSamplerClient {

    // ======================   测试
    //private static final String appId = "1830891706061811713";
    //private static  String app_secret = "bSsXY9fA3IM31YuptH7BJMp9njNR1g6s";
    //private static final String openId = "yArsjH2SLXvd0MZWth";
    //private static final String access_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NDIyMTkwNjEsInVzZXJuYW1lIjoiWHNZTzNkMk15QWxPRDJIMTRRIn0.FE_NXjBZ1Edg5F2Fva9ETpHPyI6zLx7ZkGyqvDvfz9k";
    //private static  String url = MerInfoConstant.OMS_TEST_URL;



    // ======================   生产
    private static final String appId = "1834874999361183746";
    private static  String app_secret = "gmKcDto2BX53KnJVZXRvIvPxozPazeQj";
    private static final String openId = "zs6CL5wQ8lad1pMmo5";
    private static final String access_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3NDM1MTM0MzEsInVzZXJuYW1lIjoienM2Q0w1d1E4bGFkMXBNbW81In0.sD9berIUjmodKGNELgvxmxkjkIvHvSW4al334pamxaI";
    private static  String url = "https://gateway.serviceshare.com/cloud-caring/openapi/";



    public static void main(String[] args) {
        PayCashierTest j = new PayCashierTest();
        try {
            System.out.println(j.runPaymentTest(access_token));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public  String runPaymentTest(String token) throws Exception{


        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 获取当前日期时间
        LocalDateTime now = LocalDateTime.now();

        Map<String, String> map1 = new HashMap<>();
        map1.put("app_id",appId);
        map1.put("access_token",token);
        map1.put("open_id",openId);
        map1.put("outer_order_no","" + SnowflakeIdGeneratorFactory.getInstance().nextId());
        map1.put("order_amt",1L+"");
        map1.put("reason","压测");
        map1.put("notify_url","http://localhost:8081/notify/pay_notify");
        map1.put("expire_time",now.plusMinutes(2).format(formatter));
        map1.put("order_time",now.format(formatter));
        map1.put("timestamp",System.currentTimeMillis()+"");
        String sign = MD5SignUtils.getSign(app_secret, map1);
        map1.put("sign", sign);
        //System.out.println("请求数据：" + map);
        String reqStr = JSON.toJSONString(map1);
        return HttpClientHelper.sendPost(url.concat("/pay/cashier"), "application/json", reqStr, "POST", CHARSET_UTF_8);
    }

    //设置可用参数及的默认值；
    @Override
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("access_token", access_token);//设置参数，员工编号
        return params;
    }

    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult sampleResult = new SampleResult();
        String resData = "";
        try {
            sampleResult.sampleStart();
            String accessToken = javaSamplerContext.getParameter("access_token");
            resData = runPaymentTest(accessToken);
            //断言
            JSONObject jsonObject = JSONObject.parseObject(resData);

            if (Long.parseLong(jsonObject.get("code").toString()) == 200L ||  jsonObject.get("message").toString().contains("RequestNotPermitted")) {
                sampleResult.setResponseOK();
                sampleResult.setResponseData(true + " "+resData, SampleResult.TEXT);
                //System.out.println("获取收银台URL结果:" + true);
            } else {
                sampleResult.setSuccessful(false);
                sampleResult.setResponseData(false + " " + resData, SampleResult.TEXT);
                //System.out.println("获取收银台URL结果:" + false + " " + resData);
            }
        } catch (Exception e) {
            sampleResult.setSuccessful(false);
            sampleResult.setResponseData(resData + "-----失败原因: " + e.getMessage(), SampleResult.TEXT);
            sampleResult.setResponseMessage(e.toString());
        } finally {
            sampleResult.sampleEnd();
        }

        return sampleResult;
    }


}
